<template>
	<view class="container">
		<!-- 地址列表 -->
		<scroll-view 
			scroll-y 
			class="address-scroll"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="address-list">
				<view 
					class="address-item" 
					v-for="(item, index) in addressList" 
					:key="index"
					@click="selectAddress(item)"
				>
					<view class="info">
						<view class="user-info">
							<text class="name">{{item.name}}</text>
							<text class="phone">{{item.phone}}</text>
							<text class="tag" v-if="item.isDefault">默认</text>
						</view>
						<text class="address">{{item.province}}{{item.city}}{{item.district}}{{item.address}}</text>
					</view>
					<view class="actions">
						<view class="action-btn" @click.stop="editAddress(item)">
							<text class="icon">✏️</text>
							<text>编辑</text>
						</view>
						<view class="action-btn" @click.stop="deleteAddress(item)">
							<text class="icon">🗑️</text>
							<text>删除</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="addressList.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无收货地址</text>
			</view>
		</scroll-view>
		
		<!-- 添加地址按钮 -->
		<view class="add-btn" @click="addAddress">
			<text class="icon">➕</text>
			<text>新增收货地址</text>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

// 是否还有更多数据
const hasMore = ref(true)

// 是否正在刷新
const isRefreshing = ref(false)

// 地址列表
const addressList = ref([
	{
		id: 1,
		name: '张三',
		phone: '13800138000',
		province: '四川省',
		city: '成都市',
		district: '武侯区',
		address: '天府大道1号',
		isDefault: true
	},
	{
		id: 2,
		name: '张三',
		phone: '13800138000',
		province: '四川省',
		city: '成都市',
		district: '锦江区',
		address: '春熙路2号',
		isDefault: false
	}
])

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多地址的接口调用
	setTimeout(() => {
		hasMore.value = false
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	// 重置列表并重新加载
	addressList.value = []
	hasMore.value = true
	
	setTimeout(() => {
		isRefreshing.value = false
		loadMore()
	}, 1000)
}

// 选择地址
const selectAddress = (address) => {
	// 如果是从订单确认页进入，则选择地址后返回
	const pages = getCurrentPages()
	const prevPage = pages[pages.length - 2]
	if (prevPage && prevPage.route === 'pages/order/confirm') {
		uni.$emit('selectAddress', address)
		uni.navigateBack()
	}
}

// 添加地址
const addAddress = () => {
	uni.navigateTo({
		url: '/pages/address/edit'
	})
}

// 编辑地址
const editAddress = (address) => {
	uni.navigateTo({
		url: `/pages/address/edit?id=${address.id}`
	})
}

// 删除地址
const deleteAddress = (address) => {
	uni.showModal({
		title: '提示',
		content: '确定删除该地址吗？',
		success: (res) => {
			if (res.confirm) {
				// 这里可以添加删除地址的接口调用
				uni.showToast({
					title: '删除成功',
					icon: 'success'
				})
				// 从列表中移除
				addressList.value = addressList.value.filter(item => item.id !== address.id)
			}
		}
	})
}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
	padding-bottom: 120rpx;
}

/* 地址列表样式 */
.address-scroll {
	height: calc(100vh - 120rpx);
}

.address-list {
	padding: 20rpx;
}

.address-item {
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
}

.info {
	margin-bottom: 20rpx;
}

.user-info {
	display: flex;
	align-items: center;
	margin-bottom: 10rpx;
}

.name {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	margin-right: 20rpx;
}

.phone {
	font-size: 28rpx;
	color: #666;
}

.tag {
	font-size: 24rpx;
	color: #D02B29;
	background-color: #FFF0F0;
	padding: 4rpx 16rpx;
	border-radius: 20rpx;
	margin-left: 20rpx;
}

.address {
	font-size: 28rpx;
	color: #666;
	line-height: 1.5;
}

.actions {
	display: flex;
	justify-content: flex-end;
	gap: 30rpx;
}

.action-btn {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	color: #666;
}

.action-btn .icon {
	font-size: 32rpx;
	margin-right: 10rpx;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 24rpx;
	color: #999;
}

/* 空状态样式 */
.empty-state {
	padding: 100rpx 0;
	text-align: center;
}

.empty-img {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}

/* 添加按钮样式 */
.add-btn {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	height: 100rpx;
	background-color: #D02B29;
	color: #FFFFFF;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.add-btn .icon {
	font-size: 40rpx;
	margin-right: 10rpx;
}
</style> 